package net.lovesrc.allin.biz.DogDog;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.http.MyHttpRequest;
import com.zhouzhou.fastjava.http.Requests;
import net.lovesrc.allin.biz.BaseService;
import net.lovesrc.allin.biz.gogohome.HomeAccountQueue;
import net.lovesrc.allin.biz.idcard.IdCradFromFileService;
import net.lovesrc.allin.utils.ImageUtils;
import net.lovesrc.allin.utils.MyRandomUtils;
import net.lovesrc.support.service.imageverifycode.FeiFeiImageVerifyCodeService;
import net.lovesrc.support.service.imageverifycode.ImageVerifyCodeService;
import net.lovesrc.support.service.mobileverifycode.MobileVerifyCodeService;
import net.lovesrc.support.service.mobileverifycode.MobileVerifyCodeWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Optional;

@Component
public class DogDogRegister extends BaseService {

    @Autowired
    private MobileVerifyCodeService noNameMobileVerifyCodeService;
    ImageVerifyCodeService feiFeiImageVerifyCodeService = new FeiFeiImageVerifyCodeService("129620", "yskzacEAOuKmAYMyaIBPd4ZNWaQkKkyY");

    public void register(String inviteCode) {

        CookieStore cookieStore = new BasicCookieStore();
        HttpClient httpClient = newHttpClientBuilder( cookieStore).build();

        String mobile = doGetNewMobile(null);

        byte[] bytes = MyHttpRequest.get(httpClient,"http://doge.oust.store/service/captcha.html").execute().asBytes();
        String recoginze = feiFeiImageVerifyCodeService.recoginze("10300", "image/png", new ByteArrayInputStream(bytes));
        JSONObject jsonObject1 = MyHttpRequest.post(httpClient,"http://doge.oust.store/service/sms")
                .param("captcha", recoginze)
                .param("action", "signup")
                .param("mobile", mobile)
                .execute().asJson();

        if (jsonObject1 == null || !"200".equals(jsonObject1.getString("code"))) {
            return;
        }

        String loginVerifyCode = getMobileVerifyCodeWrapper().getVerifyCodeFromPhone(mobile, 6);

        JSONObject jsonObject = MyHttpRequest.post(httpClient,"http://doge.oust.store/service/sms_check")
                .param("verify_code", loginVerifyCode)
                .param("mobile", mobile)
                .execute().asJson();

        if (jsonObject == null || !"200".equals(jsonObject.getString("code"))) {
            return;
        }

  /*      Requests.post("http://doge.oust.store/account/check")
                .param("verify_code", loginVerifyCode)
                .param("mobile", inviteCode)
                .execute();*/

        String password = MyRandomUtils.randomComplexPassword();

        JSONObject jsonObject2 = MyHttpRequest.post(httpClient,"http://doge.oust.store/signup")
                .param("username", mobile)
                .param("verify_code", loginVerifyCode)
                .param("inviter", inviteCode)
                .param("password", password)
                .param("safeword", password)
                .execute().asJson();

        if (jsonObject2 == null || !"200".equals(jsonObject2.getString("code"))) {
            return;
        }
        String uid = jsonObject2.getJSONObject("data").getJSONObject("account").getString("uid");


        JSONObject newRow = new JSONObject()
                .fluentPut("userId", uid)
                .fluentPut("fatherId", inviteCode)
                .fluentPut("card_id", "0")
                .fluentPut("coin_number", "")
                .fluentPut("phone_number", mobile)
                .fluentPut("password",password)
                .fluentPut("create_time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                .fluentPut("sign_time", "0");
        insertRow(jdbcTemplate, "t_gougou_user_info", newRow);


        JSONObject jsonObject3 = MyHttpRequest.post(httpClient,"http://doge.oust.store/signin")
                .param("username", mobile)
                .param("password", password)
                .execute().asJson();

        if (jsonObject3 == null || !"200".equals(jsonObject3.getString("code"))) {
            return;
        }


        String[] ss = IdCradFromFileService.getIdCard();
        MyHttpRequest.post(httpClient,"http://doge.oust.store/account/authen")
                .contentType("multipart/form-data")
                .param("nikename", uid)
                .param("realname", ss[0])
                .param( "idcard", ss[1])
                .execute().asJson();




    }

    private String doGetNewMobile(String phone) {
        for (; ; ) {
            String mobile = getMobileVerifyCodeWrapper().getPhone(phone);
//            if (jdbcTemplate.queryForObject("select count(1) from ERCX_USER_INFO where username = ?", long.class, mobile) > 0) {
//                getMobileVerifyCodeWrapper().addBlack(mobile);
//                continue;
//            }
            return mobile;
        }
    }

    private MobileVerifyCodeWrapper getMobileVerifyCodeWrapper() {
        return new MobileVerifyCodeWrapper(noNameMobileVerifyCodeService, "22337");
    }



}
